package test.jusfy.com.myapplication;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ListFragment} interface
 * to handle interaction events.
 * Use the {@link ListFragment} factory method to
 * create an instance of this fragment.
 */
public class ListFragment extends Fragment {

    View view;
    private Cursor cursor;
    private SimpleCursorAdapter adapter;

    private ListView listView;

    SQLDataBaseManager sqlDataBaseManager;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_list, container, false);
        listView = view.findViewById(R.id.lsv_list);

        //sqlDataBaseManager = new SQLDataBaseManager();
        //SQLiteDatabase db = sqlDataBaseManager.openDataBase(getActivity());
        //cursor = db.rawQuery("select id AS _id,loc,len,mp3loc,mp3len,songname,language,singer,search,M,L,searchsinger,midiname,mp3name,mtvname,dbpath,ensongname,ensingername,encomposer,enwriter,searchcomposer from songtable",null);
        String sql = "select id AS _id,loc,len,mp3loc,mp3len,songname,language,singer,search,M,L,searchsinger,midiname,mp3name,mtvname,dbpath,ensongname,ensingername,encomposer,enwriter,searchcomposer from songtable";
        cursor = MyApplication.query(sql,null);
        adapter = new SimpleCursorAdapter(getActivity(), R.layout.item_list_layout,cursor,new String[]{"_id","loc","songname","ensongname"},new int[]{R.id.tvID,R.id.tvLoc,R.id.tvLen,R.id.tvMp3loc}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                //获得点击的位置
                cursor.moveToPosition(position);
                String _id = cursor.getString(cursor.getColumnIndex("_id"));
                //id = cursor.getString(cursor.getColumnIndex("id"));
                String loc = cursor.getString(cursor.getColumnIndex("loc"));
                String len = cursor.getString(cursor.getColumnIndex("len"));
                String mp3loc = cursor.getString(cursor.getColumnIndex("mp3loc"));
                String mp3len= cursor.getString(cursor.getColumnIndex("mp3len"));
                String songname = cursor.getString(cursor.getColumnIndex("songname"));
                String language = cursor.getString(cursor.getColumnIndex("language"));
                String singer = cursor.getString(cursor.getColumnIndex("singer"));
                String search = cursor.getString(cursor.getColumnIndex("search"));
                String M = cursor.getString(cursor.getColumnIndex("M"));
                String L = cursor.getString(cursor.getColumnIndex("L"));
                String searchsinger = cursor.getString(cursor.getColumnIndex("searchsinger"));
                String midiname = cursor.getString(cursor.getColumnIndex("midiname"));
                String mp3name = cursor.getString(cursor.getColumnIndex("mp3name"));
                String ensongname = cursor.getString(cursor.getColumnIndex("ensongname"));
                String ensingername = cursor.getString(cursor.getColumnIndex("ensingername"));
                String encomposer = cursor.getString(cursor.getColumnIndex("encomposer"));
                String enwriter = cursor.getString(cursor.getColumnIndex("enwriter"));
                String searchcomposer = cursor.getString(cursor.getColumnIndex("searchcomposer"));
                //"id","loc","len","mp3loc","mp3len","songname","language","singer","search","M","L","searchsinger","midiname","mp3name","mtvname","dbpath","ensongname","ensingername","encomposer","enwriter","searchcomposer"
                SongTable songTable = new SongTable(_id,loc,len,mp3loc,mp3len,songname,language,singer,search,M,L,searchsinger,midiname,"","","",ensongname,ensingername,encomposer,enwriter,searchcomposer);
                Intent intent = new Intent(getActivity(),DetailActivity.class);
                intent.putExtra("songtable",songTable);
                getActivity().startActivity(intent);
            }
        });
        return view;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if(cursor!=null){
            cursor.close();
        }
    }
}
